TARGETS= guess aix bsd c89 freebsd generic linux linux-readline macosx mingw posix solaris

TARGET= guess

# win linux darwin unix android 
PLATFORM= win
#i386 x64 armv7 arm64 mips
ARCH= arch
CC= cl
AR= ar rcu
#编译标志
CFLAGS= -O2 -Wall -Wextra -DLUA_COMPAT_5_3 
#链接标志
LDFLAGS= 
#lua核心类库标志
LIBFLAGS=-fPIC -shared

LIBS= -lm 

#包含目录
INCLUDE=-I ./lua -I ./include
SDL_INCLUDE=

RM= del

CMCFLAGS= -Os

#输出目录
OUTPUT_TARGET=../builder/$(PLATFORM)-$(ARCH)
ifeq ($(PLATFORM), win)
	OUTPUT_TARGET=..\builder\$(PLATFORM)-$(ARCH)
endif

CORE_C = $(wildcard ./lua/*.c)
CORE_O= $(patsubst %.c,%.o,$(CORE_C))
EXLIB_C = $(wildcard ./exlib/*.c)
EXLIB_O= $(patsubst %.c,%.o,$(EXLIB_C))
SDL_C = $(wildcard ./sdl/*.c)
SDL_O= $(patsubst %.c,%.o,$(SDL_C))
LUA_E=	./luaexec/lua
LUA_C=	./luac/luac
ALL_BASE_OBJ=$(CORE_O) $(EXLIB_O) $(SDL_O)

OUTNAME_LUALIB=liblua
OUTNAME_LUAC=luac
OUTNAME_LUA=lua
OUTNAME_LUACL=crosslua

# Targets start here.
default: $(TARGET)

builder: clean output_info newdir core lua_lib lua_exec lua_c lua_cl
	@echo "crosslua build ok!"

core:$(CORE_O) $(SDL_O) $(EXLIB_O) lua_c_e_o
	@echo "build base obj."
#	$(RM) $(OUTPUT_TARGET)

newdir:
	if not exist "$(OUTPUT_TARGET)" mkdir  $(OUTPUT_TARGET)

#	cd .. && cd builder && mkdir $(PLATFORM)-$(ARCH)

lua_lib:
	$(AR) $(LIBS) $(CORE_O) $(LDFLAGS) $(LIBFLAGS) -o $(OUTPUT_TARGET)/$(OUTNAME_LUALIB)
	@echo "liblua build ok!"

lua_exec:
	$(AR) $(LUA_E).o $(CORE_O) $(LDFLAGS) -o $(OUTPUT_TARGET)/$(OUTNAME_LUA)
	@echo "lua build ok!"

lua_c: 
	$(AR) $(LUA_C).o $(CORE_O) $(LDFLAGS) -o $(OUTPUT_TARGET)/$(OUTNAME_LUAC)
	@echo "luac build ok!"

lua_cl:
	$(AR) $(CORE_O) $(SDL_O) $(EXLIB_O) $(LDFLAGS) -o $(OUTPUT_TARGET)/$(OUTNAME_LUACL)
	@echo "cl build ok!"

#清理构建

clean:
	if exist "$(OUTPUT_TARGET)" rmdir /s /q $(OUTPUT_TARGET)
	del /q .\lua\*.o
	del /q .\luac\*.o
	del /q .\luaexec\*.o
	del /q .\sdl\*.o
	del /q .\exlib\*.o

depend:
	@$(CC) $(CFLAGS) -MM l*.c


# Convenience targets for popular platforms.
ALL_BUILDER= builder

guess: help
	@echo Guessing

help:
	@echo "Do 'make TARGET' where TARGET is one of these:"
	@echo "   $(TARGETS)"
	@echo "See doc/readme.html for complete instructions."

AIX aix:
	$(MAKE) $(ALL_BUILDER) "PLATFORM=aix" CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" LIBS="-ldl" LDFLAGS="-brtl -bexpall"

bsd:
	$(MAKE) $(ALL_BUILDER) "PLATFORM=bsd" CFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" LIBS="-Wl,-E"

c89:
	$(MAKE) $(ALL_BUILDER) "PLATFORM=c89" CFLAGS="-DLUA_USE_C89" CC="gcc -std=c89"
	@echo ''
	@echo '*** C89 does not guarantee 64-bit integers for Lua.'
	@echo ''

FreeBSD NetBSD OpenBSD freebsd:
	$(MAKE) $(ALL_BUILDER) "PLATFORM=freebsd" CFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE -I/usr/include/edit" LIBS="-Wl,-E -ledit" CC="cc"

generic: $(ALL_BUILDER)

Linux linux: linux-noreadline

linux-noreadline:
	$(MAKE) $(ALL_BUILDER) "PLATFORM=linux" CFLAGS="-DLUA_USE_LINUX" LIBS="-Wl,-E -ldl"

linux-readline:
	$(MAKE) $(ALL_BUILDER) "PLATFORM=linux" CFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE" LIBS="-Wl,-E -ldl -lreadline"

Darwin macos macosx:
	$(MAKE) $(ALL_BUILDER) "PLATFORM=darwin" CFLAGS="-DLUA_USE_MACOSX -DLUA_USE_READLINE" LIBS="-lreadline"

mingw-i386:
	$(MAKE) $(ALL_BUILDER) "OUTNAME_LUALIB=lualib.dll" "OUTNAME_LUA=lua.exe" "OUTNAME_LUAC=luac.exe" "OUTNAME_LUACL=crosslua.exe" \
	"AR=$(CC)" "ARCH=i386" \
	CFLAGS="-DLUA_BUILD_AS_DLL -m32" "LIBS=" LDFLAGS="-s -m32"

mingw-x64:
	$(MAKE) $(ALL_BUILDER) "OUTNAME_LUALIB=lualib.dll" "OUTNAME_LUA=lua.exe" "OUTNAME_LUAC=luac.exe" "OUTNAME_LUACL=crosslua.exe" \
	"AR=$(CC)" "ARCH=x64" \
	CFLAGS="-DLUA_BUILD_AS_DLL" "LIBS=" LDFLAGS=-s"

mingw-armv7:
	$(MAKE) $(ALL_BUILDER) "OUTNAME_LUALIB=lualib.dll" "OUTNAME_LUA=lua.exe" "OUTNAME_LUAC=luac.exe" "OUTNAME_LUACL=crosslua.exe" \
	"AR=armv7-w64-mingw32-gcc -std=gnu99" "CC=armv7-w64-mingw32-gcc -std=gnu99" "ARCH=armv7" \
	"CFLAGS=-DLUA_BUILD_AS_DLL" "LIBS=" "LDFLAGS=-s"

posix:
	$(MAKE) $(ALL_BUILDER) "PLATFORM=posix" CFLAGS="-DLUA_USE_POSIX"

SunOS solaris:
	$(MAKE) $(ALL_BUILDER) "PLATFORM=solaris" CFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_REENTRANT" LIBS="-ldl"


output_info:
	@echo "target= $(TARGET)"
	@echo "platform= $(PLATFORM)"
	@echo "arch= $(ARCH)"
	@echo "cc= $(CC)"
	@echo "cflags= $(CFLAGS)"
	@echo "ldflags= $(LDFLAGS)"
	@echo "libs= $(LIBS)"
	@echo "ar= $(AR)"
	@echo "rm= $(RM)"

lua_c_e_o:
	$(CC) $(INCLUDE) $(CFLAGS) -c $(LUA_E).c -o $(LUA_E).o
	$(CC) $(INCLUDE) $(CFLAGS) -c $(LUA_C).c -o $(LUA_C).o

$(filter %.o,$(CORE_O)): %.o:%.c
	$(CC) $(INCLUDE) $(CFLAGS) -o $@ -c $^

$(filter %.o,$(SDL_O)): %.o:%.c
	$(CC) $(INCLUDE) $(SDL_INCLUDE) $(CFLAGS) -o $@ -c $^

$(filter %.o,$(EXLIB_O)): %.o:%.c
	$(CC) $(INCLUDE) $(CFLAGS) -o $@ -c $^

